package com.project.fortuneteller.controller;

import com.project.fortuneteller.config.RedisConstant;
import com.project.fortuneteller.context.UserContext;
import com.project.fortuneteller.pojo.vo.LoginFaceVO;
import com.project.fortuneteller.pojo.vo.RegisterFaceVO;
import com.project.fortuneteller.pojo.vo.UserEditorVo;
import com.project.fortuneteller.pojo.vo.UserLoginVO;
import com.project.fortuneteller.service.UserService;
import com.project.fortuneteller.utils.Result;
import io.lettuce.core.RedisClient;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping
public class UserController {

    /**
     * 模拟用户 登录
     */
    @Resource
    private UserService userService;

    @Resource
    private RedissonClient redissonClient;

    /**
     *
     * @param userVo
     * @return
     */

    @RequestMapping("/login")
    public Result login(  @RequestBody UserLoginVO userVo) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        // 参数非空校验
        if (userVo == null) {
            return Result.error("请求参数不能为空");
        }
        if (userVo.getEmail() == null || userVo.getEmail().trim().isEmpty()) {
            return Result.error("邮箱不能为空");
        }
        if (userVo.getPassword() == null || userVo.getPassword().trim().isEmpty()) {
            return Result.error("密码不能为空");
        }

        String token = UserContext.getUserInfo().getToken();
        return userService.login(userVo, token);
    }
    /**
     * 人脸上传
     * */
    @PostMapping("/registerFace")
    public Result imageLoadAndRegisterFace(@RequestBody RegisterFaceVO registerFaceVO) {
        // 参数非空校验
        if (registerFaceVO == null) {
            return Result.error("请求参数不能为空");
        }
        if (registerFaceVO.getUserId() == null || registerFaceVO.getUserId().trim().isEmpty()) {
            return Result.error("用户ID不能为空");
        }
        if (registerFaceVO.getBase64String() == null || registerFaceVO.getBase64String().trim().isEmpty()) {
            return Result.error("图片数据不能为空");
        }

        return userService.imageLoadAndRegisterFace(registerFaceVO);
    }

    // 获取当前在线人数
    @ApiOperation( "获取在线人数")
    @RequestMapping("/online")
    public Result online() {
        // zadd命令。有序列表处理
        RScoredSortedSet<String> sortedSet = redissonClient.getScoredSortedSet(RedisConstant.DEV_INLINE_PERSON_NUM);
        log.info("当前时间戳: {}", System.currentTimeMillis());
        int count = sortedSet.count(System.currentTimeMillis(),
                Boolean.TRUE,
                Double.POSITIVE_INFINITY,
                Boolean.TRUE);

        return Result.success(count);
    }

    /**
     * 人脸登录
     * */
    @PostMapping("/loginFace")
    public Result loginFace(@RequestBody LoginFaceVO loginFaceVO) {
        return userService.loginFace(loginFaceVO);
    }

    /**
     *
     * @param userVo
     * @return
     */
    @RequestMapping("/register")
    public Result register(@RequestBody UserLoginVO userVo) throws NoSuchAlgorithmException {
        // 参数非空校验
        if (userVo == null) {
            return Result.error("请求参数不能为空");
        }
        if (userVo.getEmail() == null || userVo.getEmail().trim().isEmpty()) {
            return Result.error("邮箱不能为空");
        }
        if (userVo.getPassword() == null || userVo.getPassword().trim().isEmpty()) {
            return Result.error("密码不能为空");
        }

        Result result = userService.register(userVo);
        log.info("注册成功!");

        return result;
    }

    /**
     *
     * @param
     * @return
     */
    @RequestMapping("/logout")
    public Result logout() {
        String token = UserContext.getUserInfo().getToken();
        if (token == null || token.trim().isEmpty()) {
            return Result.error("Token不能为空");
        }
        return userService.logout(token);
    }

    /**
     * 获取 httpRequest 对象
     * @return
     */
    @RequestMapping("/getUserInfo")
    public Result getUserInfo() {
        String token = UserContext.getUserInfo().getToken();
        if (token == null || token.trim().isEmpty()) {
            return Result.error("Token不能为空");
        }
        Result userInfo = userService.getUserInfo(token);
        log.info("获取用户信息: {}", token);
        return userInfo;
    }

    /**
     * 获取用户数量
     * @return
     */
    @RequestMapping("/getUserNum")
    public Result getCurrentUserCount(){
        log.info("获取用户数量");
        return Result.success(userService.getCurrentUserCount());
    }

    /**
     * 编辑用户 的 ID
     * @param userEditorVo
     * @return
     */
    @RequestMapping("/editUser")
    public Result editorAndSave(  @RequestBody UserEditorVo userEditorVo) {
        // 参数非空校验
        if (userEditorVo == null) {
            return Result.error("请求参数不能为空");
        }
        String token = UserContext.getUserInfo().getToken();
        if (token == null || token.trim().isEmpty()) {
            return Result.error("Token不能为空");
        }
        userEditorVo.setToken(token);
        return Result.success(userService.editorAndSave(userEditorVo));
    }



}
